package com.sdsy.blockchaintrainingback.handler;

import com.github.dockerjava.api.exception.DockerException;
import com.sdsy.blockchaintrainingback.exception.ServiceException;
import com.sdsy.blockchaintrainingback.pojo.common.CommonResult;
import com.sdsy.blockchaintrainingback.service.docker.DockerService;
import org.apache.hc.client5.http.HttpHostConnectException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import static com.sdsy.blockchaintrainingback.exception.enums.GlobalErrorCodeConstants.DOCKER_CONNECTED_ERROR;

/**
 * @Description TODO
 * @Author linzili
 * @Date 2023/9/26 14:14
 * @Version 1.0
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = ServiceException.class)
    public CommonResult<?> serviceExceptionHandler(ServiceException ex) {
        return CommonResult.error(ex.getCode(),ex.getMessage());
    }

    @ExceptionHandler(value = DockerException.class)
    public CommonResult<?> dockerExceptionHandler(DockerException ex) {
        return CommonResult.error(ex.getHttpStatus(),ex.getMessage());
    }

    @ExceptionHandler(value = HttpHostConnectException.class)
    public CommonResult<?> httpHostConnectExceptionHandler(HttpHostConnectException ex) {
        System.out.println(ex);
        return CommonResult.error(DOCKER_CONNECTED_ERROR);
    }
}
